﻿using LLSSample;
using SQLite;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.ServiceModel.Channels;
using Windows.Storage;
using WisdriContacts.Model;
using WisdriContacts.Resources;

namespace WisdriContacts.ViewModels
{


    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.Items = new ObservableCollection<PERSON>();
            this.Departments = new ObservableCollection<string>();
            this.PersonList = new ObservableCollection<PERSON>();

            this.DepartmentsByGroup = new List<AlphaKeyGroup<string>>();
            this.PersonListByGroup = new List<AlphaKeyGroup<PERSON>>();
        }

        public readonly string DB_PATH =
          Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "contacts.db"));

        /// <summary>
        /// ItemViewModel 对象的集合。
        /// </summary>
        public ObservableCollection<PERSON> Items { get; private set; }

        public ObservableCollection<string> Departments { get; private set; }

        public ObservableCollection<PERSON> PersonList { get; set; }

        public List<AlphaKeyGroup<string>> DepartmentsByGroup { get; set; }

        public List<AlphaKeyGroup<PERSON>> PersonListByGroup { get; set; }

        /// <summary>
        /// 返回本地化字符串的示例属性
        /// </summary>
        public string LocalizedSampleProperty
        {
            get { return AppResources.SampleProperty; }
        }

        public string SubTitle { get; set; }

        public bool IsDataLoaded { get; private set; }

        /// <summary>
        /// 创建一些 ItemViewModel 对象并将其添加到 Items 集合中。
        /// </summary>
        public void LoadData()
        {
            bool isActived;
            IsolatedStorageSettings.ApplicationSettings.TryGetValue<bool>("IsActived", out isActived);
            if (!isActived)
            {
                return;
            }


            using (SQLiteConnection conn = new SQLiteConnection(DB_PATH))
            {
                var result = conn.Query<PERSON>("select * from PERSON");

                Items.Clear();
                result.ForEach(p => this.Items.Add(p));
                Departments.Clear();
                result.Select(p => p.DEPARTMENT).Distinct().ToList().ForEach(p => this.Departments.Add(p));

                DepartmentsByGroup.Clear();
                AlphaKeyGroup<string>.CreateGroups(
                    Departments,
                    System.Threading.Thread.CurrentThread.CurrentUICulture,
                    s => s,
                    true).ForEach(p => DepartmentsByGroup.Add(p));
            }

            this.IsDataLoaded = true;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}